package com.link.base.base.message.push.service;

import com.link.base.base.message.push.model.PushInfo;
import com.link.core.cllog.LCLogger;
import com.link.core.util.RedisUtil;
import com.link.core.util.redisclient.LinkRedisClient;
import com.vivo.push.sdk.notofication.Message;
import com.vivo.push.sdk.notofication.Result;
import com.vivo.push.sdk.server.Sender;
import org.springframework.stereotype.Component;

/**
 * VIVO推送
 *
 * @author ouyangzhenqiang
 * @date 2019/08/12 16:03
 */
@Component
public class VivoPushServiceImpl {

    /**
     * 推送消息
     *
     * @author ouyangzhenqiang
     * @date 2019/08/12 16:13
     */
    void vivoPush(PushInfo record) throws Exception {
        LinkRedisClient j = null;
        try {
            j = RedisUtil.getJedis();
            String redisKey = "APP_PUSH_CONFIG";
            String vivoPushAppId = j.hget(redisKey, "vivoPushAppId_" + record.getAppId());
            String vivoPushAppKey = j.hget(redisKey, "vivoPushAppKey_" + record.getAppId());
            String vivoPushAppSecret = j.hget(redisKey, "vivoPushAppSecret_" + record.getAppId());
            // 注册登录开发平台网站获取到的appSecret
            Sender sender = new Sender(vivoPushAppSecret);
            // 注册登录开发平台网站获取到的appId和appKey
            Result result = sender.getToken(Integer.parseInt(vivoPushAppId), vivoPushAppKey);
            Sender senderMessage = new Sender(vivoPushAppSecret, result.getAuthToken());
            // 该测试手机设备订阅推送后生成的regId
            Message singleMessage = new Message.Builder().regId(record.getPushServiceToken())
                .notifyType(3)
                .title(record.getPushTitle())
                .content(record.getPushNote())
                .timeToLive(86400)
                .skipType(1)
                .requestId(String.valueOf(record.getId()))
                .build();
            Result resultMessage = senderMessage.sendSingle(singleMessage);
            LCLogger.info().withMessageKey("vivoPush").withMessage("sendPush result => " + resultMessage).flush();
        } finally {
            RedisUtil.returnResource(j);
        }
    }

}
